import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sabre_sync_fencing/dd_core/dd_other.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../../dd_core/dd_config.dart';
import '../../../dd_core/dd_log_service.dart';
import '../../../dd_core/dd_user_service.dart';
import '../../../r.dart';
import '../controllers/dd_personal_center_controller.dart';
import '../views/dd_view_background.dart';

class DDSettingPage extends StatefulWidget {
  const DDSettingPage({Key? key}) : super(key: key);

  @override
  State<DDSettingPage> createState() => _DDSettingPageState();
}

class _DDSettingPageState extends State<DDSettingPage> {
  DDPersonalCenterController _controller =
      Get.find<DDPersonalCenterController>();

  bool _isSwitched = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DDPersonalCenterController>(
        init: _controller,
        initState: (state) {
          context.didAppearCallBack(state, () async {
            final prefs = await SharedPreferences.getInstance();
            _isSwitched = prefs.getBool(DDKey.autoTranslate) ?? false;
            setState(() {});
          });
          DDLogService().ddGa(page: "setting_page");
        },
        dispose: (state) {},
        builder: (controller) {
          return Scaffold(
            appBar: AppBar(
              backgroundColor: Color(0xFFE7F6FC),
              title: Text(
                'About',
                style: TextStyle(fontSize: 18),
              ),
              leading: IconButton(
                icon: Image.asset(R.assetsImagesHomeSecond1),
                onPressed: () {
                  Get.back();
                },
              ),
            ),
            backgroundColor: Color(0xFFE7F6FC),
            body:
            Stack(
              children: [
            Positioned.fill(
            child: Image.asset(
              R.assetsImagesPerson20,
              fit: BoxFit.fill,
            ),
          ),
            SafeArea(
              top: false,
              child: Align(
                alignment: Alignment.topCenter,
                child: SingleChildScrollView(
                  child: Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: Column(
                      children: [
                        SizedBox(
                          height: 37,
                        ),
                        GestureDetector(
                          onTap: () {},
                          child: DDViewBackGround(
                            borderRadius: 16,
                            content: ClipRRect(
                              borderRadius:
                                  BorderRadius.circular(14.0),
                              child: Image.asset(
                                R.assetsImagesAppLogo,
                                width: 88,
                                height: 88,
                              ),
                            ),
                          ),
                        ),
                        SizedBox(
                          height: 12,
                        ),
                        Text(
                          'SabreSync',
                          style: TextStyle(
                              fontSize: 16, fontWeight: FontWeight.bold),
                        ),
                        SizedBox(
                          height: 69,
                        ),
                        ProfileStyle4MenuItem(
                          title:
                              "ID: ${_controller.authModel.value.userinfo?.userId ?? ''}",
                          onTap: () {
                            _controller.copyUserId(context);
                          },
                        ),
                        SizedBox(
                          height: 14,
                        ),
                        ProfileStyle5MenuItem(
                          title: "Auto Translate",
                          switchWidget: Switch(
                            value: _isSwitched,
                            activeTrackColor: Color(0xFF53BFD7),
                            onChanged: (bool value) async {
                              _isSwitched = value;
                              final prefs =
                                  await SharedPreferences.getInstance();
                              await prefs.setBool(DDKey.autoTranslate, value);
                              setState(() {});
                            },
                          ),
                        ),
                        const SizedBox(
                          height: 108,
                        ),
                        DDSafeButton(
                          child: Container(
                            width: 297,
                            height: 50,
                            decoration: BoxDecoration(
                              color: Color(0xFF53BFD7),
                              border: Border.all(
                                color: Color(0xFF4C5961),
                                width: 1,
                              ),
                              borderRadius: BorderRadius.circular(8),
                              boxShadow: const [
                                BoxShadow(
                                  color: Colors.black,
                                  spreadRadius: 1,
                                  blurRadius: 0,
                                  offset: Offset(1, 1),
                                ),
                              ],
                            ),
                            child: const Align(
                              alignment: Alignment.center,
                              child: Text(
                                'Logout',
                                style: TextStyle(
                                  color: Color(0xFFFFFFFF),
                                  fontSize: 18,
                                ),
                              ),
                            ),
                          ),
                          onPressed: () {
                            DDLogService().ddGa(page: "logout");
                            Get.find<DDUserService>().logout();
                          },
                        ),
                        SizedBox(
                          height: 14,
                        ),
                        GestureDetector(
                          onTap: () {
                            Get.dialog(
                              DDPopAlert.title(
                                description:
                                    'Are you sure you want to delete the account?',
                                descriptionStyle: TextStyle(
                                    fontSize: 16, color: Color(0xFF222222)),
                                contentInside:
                                    EdgeInsets.only(top: 30, bottom: 30, left: 10,right: 10),
                                cancelTitle: "Return",
                                okTitle: "Yes",
                                okBtnAction: () {
                                  DDLogService().ddGa(page: "deleteaccount");
                                  Get.find<DDUserService>().logoff();
                                },
                              ),
                            );
                          },
                          child: Text(
                            "Delete Account",
                            style: TextStyle(
                              fontSize: 14,
                              color: Color(0x80000000),
                              decoration: TextDecoration.underline,
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ),]),
          );
        });
  }
}

class ProfileStyle4MenuItem extends StatelessWidget {
  final String title;
  final VoidCallback onTap;

  ProfileStyle4MenuItem({
    required this.title,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return DDViewBackGround(
      margin: const EdgeInsets.only(top: 10, bottom: 10, left: 32, right: 32),
      content: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16.0),
        leading: Text(
          title,
          style: TextStyle(fontSize: 16, color: Color(0xFF404040)),
        ),
        trailing: Image.asset(R.assetsImagesPerson14),
        onTap: onTap,
      ),
    );
  }
}

class ProfileStyle5MenuItem extends StatelessWidget {
  final String title;
  final Widget switchWidget;

  ProfileStyle5MenuItem({
    required this.title,
    required this.switchWidget,
  });

  @override
  Widget build(BuildContext context) {
    return DDViewBackGround(
      margin: const EdgeInsets.only(top: 10, bottom: 10, left: 32, right: 32),
      content: ListTile(
          contentPadding:
              const EdgeInsets.only(top: 0, bottom: 0, left: 16, right: 8),
          leading: Text(
            title,
            style: TextStyle(fontSize: 16, color: Color(0xFF404040)),
          ),
          trailing: switchWidget),
    );
  }
}
